package com.cbh.amadeus.server.domain;

import com.cbh.amadeus.common.protocol.AmadeusMessageWrap;
import io.netty.channel.Channel;
import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author ChenBiHui
 * @date 2025/5/17
 */
@Data
public class DelayAmadeusMessageWrap implements Delayed {

    private final Channel client;
    private final AmadeusMessageWrap wrap;

    public DelayAmadeusMessageWrap(Channel client, AmadeusMessageWrap wrap) {
        this.client = client;
        this.wrap = wrap;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.wrap.getAchieve(), TimeUnit.MILLISECONDS) -
                unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed inbound) {
        return Long.compare(this.getDelay(TimeUnit.NANOSECONDS), inbound.getDelay(TimeUnit.NANOSECONDS));
    }
}
